package com.haochang.chunkvod.controller.adapter.order;

import java.util.List;

import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.model.PhotoDetailInfo;
import com.haochang.chunkvod.tools.thirdparty.image.core.DisplayImageOptions;
import com.haochang.chunkvod.tools.thirdparty.image.core.ImageLoader;
import com.haochang.chunkvod.view.thirdparty.photoview.PhotoView;
import com.haochang.chunkvod.view.thirdparty.photoview.PhotoViewAttacher.OnPhotoTapListener;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PhotoDetailAdapter extends PagerAdapter
{
	private List<PhotoDetailInfo> dataSource;
	private Activity context;
	private DisplayImageOptions options;
	private LayoutInflater layoutInflater;

	public PhotoDetailAdapter(List<PhotoDetailInfo> dataSource, Activity context)
	{
		super();
		this.dataSource = dataSource;
		this.context = context;
		layoutInflater = LayoutInflater.from(context);
		//修改 设置本地缓存 2016/12/23
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.vod_default_image).showImageForEmptyUri(R.drawable.vod_default_image)
				.showImageOnFail(R.drawable.vod_default_image).cacheOnDisc(true).cacheInMemory(true).build();
	}

	@Override
	public int getCount()
	{
		return CollectionUtils.isEmpty(dataSource) ? 0 : dataSource.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1)
	{
		return arg0 == arg1;
	}

	@Override
	public Object instantiateItem(final ViewGroup viewPager, final int position)
	{
		PhotoDetailInfo info = null;
		View root = layoutInflater.inflate(R.layout.photo_detail_item, viewPager, false);
		if (!CollectionUtils.isEmpty(dataSource) && position < dataSource.size())
		{
			info = dataSource.get(position);
		}
		final PhotoView pv = (PhotoView) root.findViewById(R.id.pv);
		pv.setTag(position);
		pv.setMaximumScale(3.0f);
		pv.setMinimumScale(1.0f);
		pv.setOnPhotoTapListener(new OnPhotoTapListener()
		{

			@Override
			public void onPhotoTap(View view, float x, float y)
			{
				context.finish();
			}
		});
		if (info != null)
		{
			ImageLoader.getInstance().displayImage(info.getUrl(), pv, options);
		}
		viewPager.addView(root);
		return root;
	}

	@Override
	public void destroyItem(ViewGroup viewPager, int position, Object object)
	{
		viewPager.removeView((View) object);
	}

}